<?php

namespace app\api\model\service;

use think\Model;
class Follow extends Model
{
    // 表名
    protected $name = 'service_follow';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function getCount($where)
    {
        return self::where($where)->count();
    }

    /**
     * 更新状态
     * @param $data
     * @param $info
     * @return true
     */
    public static function updateFollow($data,$info)
    {
        if($info)
        {
            self::where(['id'=>$info['id']])->update(['state'=>$data['state'],'updatetime'=>time()]);
        }else{
            $data['createtime'] = time();
            $data['updatetime'] = time();
            self::create($data);
        }
        return true;
    }

    public static function getState($params)
    {
        $params['state'] = 1;
        return self::where($params)->value('state');
    }

    public static function getList($params)
    {
        $list = self::where(['user_id'=>$params['user_id'],'type'=>$params['type'],'state'=>1])->field('id,type,follow_id')->order('updatetime desc')->page($params['page'])->limit(10)->select();
        foreach ($list as $key=>$value)
        {
            switch ($value['type'])
            {
                case 0:
                    $followInfo = Skill::getSkillInfo(['id'=>$value['follow_id'],'lng'=>$params['lng'],'lat'=>$params['lat']]);
                    break;
                case 1:
                    $followInfo = Goods::where(['id'=>$value['follow_id'],'status'=>'normal'])->field('id,name,image,price,tag_name,response_hour,salenums')->find();
                    break;
                case 2:
                    $followInfo = Shop::getShopInfo(['id'=>$value['follow_id'],'lng'=>$params['lng'],'lat'=>$params['lat']]);
                    break;
            }
            if(!$followInfo)
            {
                unset($list[$key]);
            }else{
                $list[$key] = $followInfo;
            }
        }
        return array_values($list);

    }

}